/*   1:    */ package com.atomikos.util;
/*   2:    */ 
/*   3:    */ import java.io.ByteArrayInputStream;
/*   4:    */ import java.io.ByteArrayOutputStream;
/*   5:    */ import java.io.IOException;
/*   6:    */ import java.io.InputStream;
/*   7:    */ import java.io.ObjectInputStream;
/*   8:    */ import java.io.ObjectOutputStream;
/*   9:    */ import java.io.ObjectStreamClass;
/*  10:    */ import java.io.Serializable;
/*  11:    */ import java.util.Hashtable;
/*  12:    */ import javax.naming.BinaryRefAddr;
/*  13:    */ import javax.naming.Context;
/*  14:    */ import javax.naming.Name;
/*  15:    */ import javax.naming.NamingException;
/*  16:    */ import javax.naming.RefAddr;
/*  17:    */ import javax.naming.Reference;
/*  18:    */ import javax.naming.spi.ObjectFactory;
/*  19:    */ 
/*  20:    */ public class SerializableObjectFactory
/*  21:    */   implements ObjectFactory
/*  22:    */ {
/*  23:    */   static Reference createReference(Serializable object, String factoryClassName)
/*  24:    */     throws NamingException
/*  25:    */   {
/*  26: 57 */     Reference ret = null;
/*  27: 58 */     BinaryRefAddr handle = null;
/*  28:    */     ByteArrayOutputStream bout;
/*  29:    */     try
/*  30:    */     {
/*  31: 62 */       bout = new ByteArrayOutputStream();
/*  32: 63 */       ObjectOutputStream out = new ObjectOutputStream(bout);
/*  33: 64 */       out.writeObject(object);
/*  34: 65 */       out.close();
/*  35:    */     }
/*  36:    */     catch (IOException e)
/*  37:    */     {
/*  38: 69 */       throw new NamingException(e.getMessage());
/*  39:    */     }
/*  40: 72 */     handle = new BinaryRefAddr("com.atomikos.serializable", bout.toByteArray());
/*  41: 73 */     ret = new Reference(object.getClass().getName(), handle, factoryClassName, null);
/*  42:    */     
/*  43: 75 */     return ret;
/*  44:    */   }
/*  45:    */   
/*  46:    */   public static Reference createReference(Serializable object)
/*  47:    */     throws NamingException
/*  48:    */   {
/*  49: 91 */     return createReference(object, SerializableObjectFactory.class.getName());
/*  50:    */   }
/*  51:    */   
/*  52:    */   public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
/*  53:    */     throws Exception
/*  54:    */   {
/*  55:104 */     Object ret = null;
/*  56:105 */     if ((obj instanceof Reference))
/*  57:    */     {
/*  58:106 */       Reference ref = (Reference)obj;
/*  59:107 */       RefAddr ra = ref.get("com.atomikos.serializable");
/*  60:108 */       if (ra != null)
/*  61:    */       {
/*  62:109 */         byte[] bytes = (byte[])ra.getContent();
/*  63:110 */         ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
/*  64:111 */         ObjectInputStream in = new ObjectInputStream(bin)
/*  65:    */         {
/*  66:    */           protected Class resolveClass(ObjectStreamClass desc)
/*  67:    */             throws IOException, ClassNotFoundException
/*  68:    */           {
/*  69:    */             try
/*  70:    */             {
/*  71:118 */               return super.resolveClass(desc);
/*  72:    */             }
/*  73:    */             catch (ClassNotFoundException ex) {}
/*  74:123 */             return Thread.currentThread().getContextClassLoader().loadClass(desc.getName());
/*  75:    */           }
/*  76:127 */         };
/*  77:128 */         ret = in.readObject();
/*  78:129 */         in.close();
/*  79:    */       }
/*  80:    */     }
/*  81:133 */     return ret;
/*  82:    */   }
/*  83:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.util.SerializableObjectFactory
 * JD-Core Version:    0.7.0.1
 */